home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UCRASM25.ARJ / STRINSM.ASM < prev    next >
Assembly Source File  |  1991-10-12  |  2KB  |  142 lines

  1. StdGrp        group    stdlib,stddata
  2. stddata        segment    para public 'sldata'
  3. stddata        ends
  4. ;
  5. stdlib        segment    para public 'slcode'
  6.         assume    cs:stdgrp
  7.         extrn    sl_malloc:far
  8. ;
  9. ;
  10. ; strinsm- Inserts one string within another.
  11. ;
  12. ; inputs:
  13. ;
  14. ;    ES:DI- Points at destination string, the one into which the source
  15. ;           string will be appended.
  16. ;
  17. ;    DX:SI- Points at the string to insert.
  18. ;
  19. ;    CX-    Index into source string (ES:DI) to begin insertion.
  20. ;
  21. ; outputs:
  22. ;
  23. ;    ES:DI-    Points at new string on the heap.
  24. ;    Carry-    Zero if no error, One if memory allocation error.
  25. ;
  26. ;
  27. ;
  28.         public    sl_strinsm
  29. ;
  30. srcseg        equ    (0-2)[bp]
  31. source        equ    (0-4)[bp]
  32. destseg        equ    (0-6)[bp]
  33. dest        equ    (0-8)[bp]
  34. insindx        equ    (0-10)[bp]
  35. ;
  36. sl_strinsm    proc    far
  37.         push    bp
  38.         mov    bp, sp
  39.         push    dx
  40.         push    si
  41.         push    es
  42.         push    di
  43.         push    cx
  44.         pushf
  45.         push    ax
  46.                 push    bx
  47.         push    ds
  48. ;
  49.         cld
  50. ;
  51. ; Compute the length of the destination string.
  52. ;
  53.         mov    al, 0
  54.         mov    cx, 0ffffh
  55.     repne    scasb
  56. ;
  57. ; Compute the length of the string to insert.
  58. ;
  59.         mov    di, si
  60.         mov    es, dx            ;(srcseg)
  61.         mov    al, 0
  62.     repne    scasb
  63.         neg    cx
  64.         dec    cx
  65. ;;;;;;;;;;;;;;    dec    cx            ;Plus one for the zero byte.
  66. ;
  67. ; Allocate memory for the new string:
  68. ;
  69.         call    sl_malloc
  70.         jnc    GoodMalloc
  71. ;
  72.         pop    ds
  73.         pop    bx
  74.         pop    ax
  75.         popf
  76.         pop    cx
  77.         pop    di
  78.         pop    es
  79.         pop    si
  80.         pop    dx
  81.         pop    bp
  82.         stc                ;Out of memory
  83.         ret
  84. ;
  85. ;
  86. ; If we were able to malloc the string, drop down here:
  87. ;
  88. GoodMalloc:    mov    dx, es            ;Save ptr to new string
  89.         mov    bx, di
  90.         lds    si, dword ptr Dest
  91. ;
  92. ; Copy the first part of the destination string to the new string.
  93. ;
  94.         mov    cx, insindx
  95. CpyDest1:    lodsb
  96.         stosb
  97.         cmp    al, 0
  98.         loopne    CpyDest1
  99.         jnz    SkipDec
  100.         dec    di            ;Back up a character if we
  101.         dec    si            ; hit a zero byte.
  102. ;
  103. SkipDec:    push si                ;Save ptr to middle of dest.
  104. ;
  105. ; Copy the source string into the middle.
  106. ;
  107.         lds    si,source
  108. CpySrc:        lodsb
  109.         stosb
  110.         cmp    al, 0
  111.         jnz    CpySrc
  112.         dec    di
  113. ;
  114. ; Copy the remainder of the destination string to the new string.
  115. ;
  116.         pop    si            ;Retrieve ptr into dest.
  117.         mov    ds, DestSeg
  118. CpyDest:    lodsb
  119.         stosb
  120.         cmp    al, 0
  121.         jnz    CpyDest
  122.         mov    es, dx            ;Retrieve ptr to new string.
  123.         mov    di, bx
  124. ;
  125.         pop    ds
  126.         pop    bx
  127.         pop    ax
  128.         popf
  129.         pop    cx
  130.         pop    si            ;Don't restore es:di
  131.         pop    si
  132.         pop    si
  133.         pop    dx
  134.         pop    bp
  135.         clc                ;Allocated string okay.
  136.         ret
  137. sl_strinsm    endp
  138. ;
  139. ;
  140. stdlib        ends
  141.         end
  142.